package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.THelp;
import com.ruoyi.system.domain.TLowairServiceFile;
import com.ruoyi.system.dto.req.TLowairServiceFileDto;
import com.ruoyi.system.dto.resq.HelpRespDto;
import com.ruoyi.system.mapper.TLowairServiceFileMapper;
import com.ruoyi.system.service.THelpService;
import com.ruoyi.system.mapper.THelpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author dell
 * @description 针对表【t_help(使用帮助信息表)】的数据库操作Service实现
 * @createDate 2023-11-13 11:08:33
 */
@Service
public class THelpServiceImpl extends ServiceImpl<THelpMapper, THelp>
        implements THelpService {

    @Autowired
    private THelpMapper tHelpMapper;

    @Autowired
    private TLowairServiceFileMapper tLowairServiceFileMapper;

    @Override
    public List<HelpRespDto> selectHelpList(String helpTitle) {

        return tHelpMapper.selectListByHelpTitle(helpTitle);
    }

    @Override
    public HelpRespDto selectHelpById(String helpId) {
        HelpRespDto helpRespDto = new HelpRespDto();
        THelp tHelp = tHelpMapper.selectById(helpId);
        BeanUtils.copyBeanProp(helpRespDto, tHelp);

        TLowairServiceFile tLowairServiceFile = tLowairServiceFileMapper.selectOne(new LambdaQueryWrapper<TLowairServiceFile>().eq(TLowairServiceFile::getBusinessId, helpId));
        TLowairServiceFileDto tLowairServiceFileDto = new TLowairServiceFileDto();
        BeanUtils.copyBeanProp(tLowairServiceFileDto, tLowairServiceFile);
        helpRespDto.setFile(tLowairServiceFileDto);

        return helpRespDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertHelp(HelpRespDto helpRespDto) {
        THelp tHelp = new THelp();
        BeanUtils.copyBeanProp(tHelp, helpRespDto);
        tHelp.setCreateTime(DateUtils.getNowDate());
        tHelp.setCreateBy(SecurityUtils.getUsername());
        tHelp.setUpdateTime(DateUtils.getNowDate());
        tHelp.setUpdateBy(SecurityUtils.getUsername());
        tHelp.setStatus(1);

        tHelpMapper.insert(tHelp);

        TLowairServiceFileDto file = helpRespDto.getFile();
        TLowairServiceFile tLowairServiceFile = new TLowairServiceFile();
        BeanUtils.copyBeanProp(tLowairServiceFile, file);
        tLowairServiceFile.setBusinessId(tHelp.getHelpId());
        tLowairServiceFile.setCreateTime(file.getCreateTime());
        tLowairServiceFile.setCreateBy(SecurityUtils.getUsername());
        tLowairServiceFile.setUpdateTime(DateUtils.getNowDate());
        tLowairServiceFile.setUpdateBy(SecurityUtils.getUsername());
        tLowairServiceFileMapper.insert(tLowairServiceFile);

        return AjaxResult.success("新增成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateHelp(HelpRespDto helpRespDto) {
        THelp tHelp = new THelp();
        BeanUtils.copyBeanProp(tHelp, helpRespDto);
        tHelp.setUpdateTime(DateUtils.getNowDate());
        tHelp.setUpdateBy(SecurityUtils.getUsername());
        tHelp.setStatus(1);

        tHelpMapper.updateById(tHelp);

        TLowairServiceFileDto file = helpRespDto.getFile();
        TLowairServiceFile tLowairServiceFile = new TLowairServiceFile();
        BeanUtils.copyBeanProp(tLowairServiceFile, file);
        tLowairServiceFile.setBusinessId(tHelp.getHelpId());

        //如果help_id为null则新增
        if (StringUtils.isNull(file.getFileId())) {
            tLowairServiceFile.setBusinessId(tHelp.getHelpId());
            tLowairServiceFile.setCreateTime(file.getCreateTime());
            tLowairServiceFile.setCreateBy(SecurityUtils.getUsername());
            tLowairServiceFile.setUpdateTime(DateUtils.getNowDate());
            tLowairServiceFile.setUpdateBy(SecurityUtils.getUsername());
            tLowairServiceFileMapper.insert(tLowairServiceFile);
        } else {
            tLowairServiceFile.setUpdateTime(DateUtils.getNowDate());
            tLowairServiceFile.setUpdateBy(SecurityUtils.getUsername());
            tLowairServiceFileMapper.updateById(tLowairServiceFile);
        }

        return AjaxResult.success("修改成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteHelpByIds(List<String> helpIds) {

        for (String helpId : helpIds) {
            tLowairServiceFileMapper.delete(new LambdaQueryWrapper<TLowairServiceFile>().eq(TLowairServiceFile::getBusinessId, helpId));
            tHelpMapper.deleteById(helpId);
        }

        return AjaxResult.success("删除成功");
    }
}




